package com.yt.phone.nanhaiju;

import com.google.gson.Gson;
import com.yt.phone.SendSmsRequest;
import lombok.Data;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.springframework.stereotype.Service;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class NhSmsService {
    public NhSmsService(){

    }
    private  String generateMD5Signature(String enterpriseNo, String account, String timestamp, String httpSignKey) throws NoSuchAlgorithmException {
        String dataToHash = enterpriseNo + account + timestamp + httpSignKey;
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(dataToHash.getBytes());
        byte[] digest = md.digest();
        StringBuilder sb = new StringBuilder();
        for (byte b : digest) {
            sb.append(String.format("%02X", b)); // Convert byte to hex
        }
        return sb.toString();
    }

    public void sendSmsCode(String phoneNumber,String code)throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        SmsInfo smsInfo = new SmsInfo();
        smsInfo.setEnterprise_no("GD10A529677");
        smsInfo.setAccount("02085125125");
        smsInfo.setPhones(phoneNumber);
        smsInfo.setSubcode("8888");
        smsInfo.setTimestamp("time");
        smsInfo.setContent("尊敬的用户，您的验证码为"+code+",该验证码2分钟内有效，请勿泄露于他人");
        smsInfo.setSign(generateMD5Signature(smsInfo.getEnterprise_no(), smsInfo.getAccount(), smsInfo.getTimestamp(), "61DEECB93DDBEA79AA06530AE7311BDD"));

        // 构造请求头
        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://14.29.202.151:8888/json/submit");
        httpPost.setHeader("Content-Type", "application/json");

        // 使用 Gson 将对象转为 JSON
        Gson gson = new Gson();
        String requestBody = gson.toJson(smsInfo);

        // 设置请求体并明确指定UTF-8编码
        StringEntity stringEntity = new StringEntity(requestBody, "UTF-8");
        httpPost.setEntity(stringEntity);
        httpClient.execute(httpPost);
    }

    public void sendSmsText(String phoneNumber,String content)throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        SmsInfo smsInfo = new SmsInfo();
        smsInfo.setEnterprise_no("GD10A529677");
        smsInfo.setAccount("02085125125");
        smsInfo.setPhones(phoneNumber);
        smsInfo.setSubcode("8888");
        smsInfo.setTimestamp("time");
        smsInfo.setContent(content);
        smsInfo.setSign(generateMD5Signature(smsInfo.getEnterprise_no(), smsInfo.getAccount(), smsInfo.getTimestamp(), "61DEECB93DDBEA79AA06530AE7311BDD"));

        // 构造请求头
        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://14.29.202.151:8888/json/submit");
        httpPost.setHeader("Content-Type", "application/json");

        // 使用 Gson 将对象转为 JSON
        Gson gson = new Gson();
        String requestBody = gson.toJson(smsInfo);

        // 设置请求体并明确指定UTF-8编码
        StringEntity stringEntity = new StringEntity(requestBody, "UTF-8");
        httpPost.setEntity(stringEntity);
        httpClient.execute(httpPost);
    }
}
